<template>
  <div class="app-container">
<div v-show="!open">

  <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="materialTitle">
        <el-input v-model="queryParams.materialTitle" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:material:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:material:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:material:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:material:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="materialTitle" />
      <el-table-column label="简介" align="center" prop="materialDigest" />
      <el-table-column label="作者" align="center" prop="materialAuthor" />
      <el-table-column label="标签id" align="center" prop="labelId" />
      <el-table-column label="封面图片URL" align="center" prop="coverUrl" />
      <el-table-column label="视频、音频URL" align="center" prop="audiovisualUrl" />
      <!-- <el-table-column label="是否发布到微信：true：同步，false：不同步" align="center" prop="reportWechat" /> -->
      <!-- <el-table-column label="微信返回的素材URL" align="center" prop="wechatUrl" /> -->
      <!-- <el-table-column label="微信返回的永久素材id" align="center" prop="wechatMediaId" /> -->
      <!-- <el-table-column label="操作人ID" align="center" prop="userId" /> -->
      <!-- <el-table-column label="是否删除：true：已删除，false：未删除" align="center" prop="isDelete" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:material:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:material:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
</div>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body> -->
     <div v-show="open" style="width: 70%;margin: 20px auto;">
  <h2 style="text-align: center;line-height: 70px;border-bottom: 1px solid #ccc;">{{ title }}</h2>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12"> <el-form-item label="标题" prop="materialTitle">
              <el-input v-model="form.materialTitle" placeholder="请输入标题" />
            </el-form-item></el-col>
          <el-col :span="12"> <el-form-item label="标签id" prop="labelId">
              <el-input v-model="form.labelId" placeholder="请输入标签id" />
            </el-form-item></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">      <el-form-item label="简介" prop="materialDigest">
            <el-input
  type="textarea"
  placeholder="请输入简介"
  v-model="form.materialDigest"
  maxlength="100"
  show-word-limit
  style="width: 500px;"
  rows="5"
>
</el-input>
        </el-form-item></el-col>
          <el-col :span="12">    <el-form-item label="封面上传" prop="coverUrl">
        <el-button type="primary" @click="choseimg()">选择封面</el-button>
        </el-form-item></el-col>
        </el-row>
   
        <el-row :gutter="20">
          <el-col :span="12">     <el-form-item label="视频上传" prop="audiovisualUrl">
            <el-upload
 
 class="upload-demo"
 drag
 :headers="headers"
 :action="url"
 :on-success="handlePreview"
 :on-remove="handleRemove"
 :before-remove="beforeRemove"
 :limit="1"
 :data="obj"
 :on-exceed="handleExceed"
 :file-list="file"
 accept=".mp4"
 >
 <!-- <el-button size="small" type="primary">点击上传</el-button> -->
 <i class="el-icon-upload"></i>
 <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
 <div class="el-upload__tip" slot="tip">只能上传mp4文件</div>
 </el-upload>
        </el-form-item></el-col>
      
        </el-row>
   
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

     </div>
    <!-- </el-dialog> -->
    <el-dialog
  title="选择图片"
  :visible.sync="dialogVisible"
  width="60%"
  :before-close="handleClose">
  <div>
    <el-upload
  class="upload-demo"
  action="https://jsonplaceholder.typicode.com/posts/"
  :on-preview="handlePrevieww"
  :on-remove="handleRemovee"
  :before-remove="beforeRemovee"
  :limit="1"
  :on-exceed="handleExceedd"
  :show-file-list="false"
  :file-list="fileList">


  <el-button size="small" type="primary">点击上传</el-button>
</el-upload>
<div class="imglist">
    <div class="imgitem" v-for="item in 10" :key="item">
{{ item }}
    </div>
  </div>
<div style="text-align: center;margin-top: 30px;">

  <el-pagination
  background
  layout="prev, pager, next"
  :total="1000">
</el-pagination>
</div>
  </div>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial } from "@/api/system/material";
import { getToken } from "@/utils/auth";

export default {
  name: "Material",
  data() {
    return {
       fileList: [],
      headers:{ Authorization: "Bearer " + getToken() },

      url:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialTitle: null,
        materialDigest: null,
        materialAuthor: null,
        labelId: null,
        coverUrl: null,
        audiovisualUrl: null,
        reportWechat: null,
        wechatUrl: null,
        wechatMediaId: null,
        userId: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      file:"",
      dialogVisible:false
    };
  },
  created() {
    this.url= process.env.VUE_APP_BASE_API + "/system/registeredUser/authenticationDataUserUpload"
    this.getList();
  },
  methods: {
      handleRemovee(file, fileList) {
        console.log(file, fileList);
      },
      handlePrevieww(file) {
        console.log(file);
      },
      handleExceedd(files, fileList) {
    
      },
      beforeRemovee(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
    handleClosee(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
    choseimg(){
this.dialogVisible=true
    },
    handleRemove(file, fileList) {
      },
      handlePreview(file) {
        let str=file.authenticationDataUser
        // if (this.subfile.length != 0) {
        //   // this.subfile.forEach(el => {
        //     let item = this.subfile.find(ele => ele.authenticationDataId == str.authenticationDataId);
				// 			if (!item) {
				// 				this.subfile.push(str)
        //       }else{
        //         this.subfile.forEach(el => {
        //           if(el.authenticationDataId==str.authenticationDataId){
        //             el.data=str.data
        //           }
        //         })
        //       }
		
				// 	}else{
        //     this.subfile.push(str)
        //   }
      },
      handleExceed(files, ) {
        this.$message.warning(`只能上传一个文件`);
      },
      beforeRemove(file, ) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materialId: null,
        materialTitle: null,
        materialDigest: null,
        materialAuthor: null,
        labelId: null,
        coverUrl: null,
        audiovisualUrl: null,
        reportWechat: null,
        wechatUrl: null,
        wechatMediaId: null,
        userId: null,
        isDelete: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.materialId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materialId = row.materialId || this.ids
      getMaterial(materialId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改视频信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materialId != null) {
            updateMaterial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaterial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialIds = row.materialId || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + materialIds + '"的数据项？').then(function () {
        return delMaterial(materialIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/material/export', {
        ...this.queryParams
      }, `material_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.imglist{
width: 100%;

  margin-top: 20px;
  display: flex;
  flex-wrap: wrap;
  .imgitem{
    width: 255px;
    height: 150px;
    border: 1px solid #ccc;
    border-radius: 10px;
    margin: 10px;
  }
}
</style>
